package br.org.etec.dotcao.utils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import br.org.etec.dotcao.exception.UserNotLoggedIn;
import br.org.etec.dotcao.model.User;

public class SessionUtils {
	public static HttpSession session(HttpServletRequest request){
		HttpSession session = request.getSession();
		
		if(session == null){
			throw new RuntimeException("Não havia sessão");
		}
		
		return session;
	}
	
	public static User logged(HttpServletRequest request) throws UserNotLoggedIn{
		HttpSession session = session(request);
		
		User user = (User) session.getAttribute(Constants.LOGGED_USER);
		
		if(user == null){
			throw new UserNotLoggedIn();
		}
		
		return user;
	}
	
	public static void putInSession(HttpServletRequest request, User user){
		session(request).setAttribute(Constants.LOGGED_USER, user);
	}
}
